<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户ID" />
      </el-form-item>
      <el-form-item label="作者sec_uid" prop="secUid">
        <el-input v-model="formData.secUid" placeholder="请输入作者sec_uid" />
      </el-form-item>
      <el-form-item label="其他唯一ID" prop="uniqueId">
        <el-input v-model="formData.uniqueId" placeholder="请输入其他唯一ID" />
      </el-form-item>
      <el-form-item label="抖音账号" prop="userShortId">
        <el-input v-model="formData.userShortId" placeholder="请输入抖音账号" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="formData.nickname" placeholder="请输入昵称" />
      </el-form-item>
      <el-form-item label="签名" prop="signature">
        <el-input v-model="formData.signature" placeholder="请输入签名" />
      </el-form-item>
      <el-form-item label="年龄" prop="userAge">
        <el-input v-model="formData.userAge" placeholder="请输入年龄" />
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-input v-model="formData.avatar" placeholder="请输入头像" />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input v-model="formData.province" placeholder="请输入省" />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input v-model="formData.city" placeholder="请输入市" />
      </el-form-item>
      <el-form-item label="国家" prop="country">
        <el-input v-model="formData.country" placeholder="请输入国家" />
      </el-form-item>
      <el-form-item label="学校" prop="schoolName">
        <el-input v-model="formData.schoolName" placeholder="请输入学校" />
      </el-form-item>
      <el-form-item label="IP属地:广东" prop="ipLocation">
        <el-input v-model="formData.ipLocation" placeholder="请输入IP属地:广东" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input v-model="formData.gender" placeholder="请输入性别" />
      </el-form-item>
      <el-form-item label="合集数量" prop="mixCount">
        <el-input v-model="formData.mixCount" placeholder="请输入合集数量" />
      </el-form-item>
      <el-form-item label="关注数量" prop="followingCount">
        <el-input v-model="formData.followingCount" placeholder="请输入关注数量" />
      </el-form-item>
      <el-form-item label="最高粉丝数量" prop="maxFollowerCount">
        <el-input v-model="formData.maxFollowerCount" placeholder="请输入最高粉丝数量" />
      </el-form-item>
      <el-form-item label="粉丝数量" prop="followerCount">
        <el-input v-model="formData.followerCount" placeholder="请输入粉丝数量" />
      </el-form-item>
      <el-form-item label="获赞数量" prop="totalFavorite">
        <el-input v-model="formData.totalFavorite" placeholder="请输入获赞数量" />
      </el-form-item>
      <el-form-item label="作品数量" prop="awemeCount">
        <el-input v-model="formData.awemeCount" placeholder="请输入作品数量" />
      </el-form-item>
      <el-form-item label="喜欢数量" prop="favoriteCount">
        <el-input v-model="formData.favoriteCount" placeholder="请输入喜欢数量" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CreatorApi, CreatorVO } from '@/api/douyin/creator'

/** 作者 表单 */
defineOptions({ name: 'CreatorForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  secUid: undefined,
  uniqueId: undefined,
  userShortId: undefined,
  nickname: undefined,
  signature: undefined,
  userAge: undefined,
  avatar: undefined,
  province: undefined,
  city: undefined,
  country: undefined,
  schoolName: undefined,
  ipLocation: undefined,
  gender: undefined,
  mixCount: undefined,
  followingCount: undefined,
  maxFollowerCount: undefined,
  followerCount: undefined,
  totalFavorite: undefined,
  awemeCount: undefined,
  favoriteCount: undefined
})
const formRules = reactive({
  userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
  secUid: [{ required: true, message: '作者sec_uid不能为空', trigger: 'blur' }],
  uniqueId: [{ required: true, message: '其他唯一ID不能为空', trigger: 'blur' }],
  userShortId: [{ required: true, message: '抖音账号不能为空', trigger: 'blur' }],
  nickname: [{ required: true, message: '昵称不能为空', trigger: 'blur' }],
  signature: [{ required: true, message: '签名不能为空', trigger: 'blur' }],
  userAge: [{ required: true, message: '年龄不能为空', trigger: 'blur' }],
  avatar: [{ required: true, message: '头像不能为空', trigger: 'blur' }],
  province: [{ required: true, message: '省不能为空', trigger: 'blur' }],
  city: [{ required: true, message: '市不能为空', trigger: 'blur' }],
  country: [{ required: true, message: '国家不能为空', trigger: 'blur' }],
  schoolName: [{ required: true, message: '学校不能为空', trigger: 'blur' }],
  ipLocation: [{ required: true, message: 'IP属地:广东不能为空', trigger: 'blur' }],
  gender: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
  mixCount: [{ required: true, message: '合集数量不能为空', trigger: 'blur' }],
  followingCount: [{ required: true, message: '关注数量不能为空', trigger: 'blur' }],
  maxFollowerCount: [{ required: true, message: '最高粉丝数量不能为空', trigger: 'blur' }],
  followerCount: [{ required: true, message: '粉丝数量不能为空', trigger: 'blur' }],
  totalFavorite: [{ required: true, message: '获赞数量不能为空', trigger: 'blur' }],
  awemeCount: [{ required: true, message: '作品数量不能为空', trigger: 'blur' }],
  favoriteCount: [{ required: true, message: '喜欢数量不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CreatorApi.getCreator(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CreatorVO
    if (formType.value === 'create') {
      await CreatorApi.createCreator(data)
      message.success(t('common.createSuccess'))
    } else {
      await CreatorApi.updateCreator(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    secUid: undefined,
    uniqueId: undefined,
    userShortId: undefined,
    nickname: undefined,
    signature: undefined,
    userAge: undefined,
    avatar: undefined,
    province: undefined,
    city: undefined,
    country: undefined,
    schoolName: undefined,
    ipLocation: undefined,
    gender: undefined,
    mixCount: undefined,
    followingCount: undefined,
    maxFollowerCount: undefined,
    followerCount: undefined,
    totalFavorite: undefined,
    awemeCount: undefined,
    favoriteCount: undefined
  }
  formRef.value?.resetFields()
}
</script>